/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import domain.Userdom;
import domain.Watercontract;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Alejandro
 */
public class WaterContractDao {
EntityManager em;
    EntityTransaction tx;

    public WaterContractDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Watercontract getWaterContract(Integer idWaterContract) {
        Watercontract w = (Watercontract) em.createNativeQuery("select * from watercontract as n where n.idWaterContract='" + idWaterContract + "'", Watercontract.class).getSingleResult();
        return w;
    }

    public void saveWaterContract(Watercontract w) {
        tx.begin();
        em.persist(w);
        tx.commit();
    }

    public void removeWaterContract(Watercontract w) {
        tx.begin();
        em.remove(w);
        tx.commit();
    }

    public List<Watercontract> getWaterContrList(Userdom user) {
        tx.begin();
        List list = em.createNativeQuery("select * from bankaccount b, contract c, watercontract w where b.userFK='"+user.getNick()+"' and c.bankFK=b.idBank and w.contractFK=c.idContract", Watercontract.class).getResultList();
        tx.commit();
        return list;
    }

    public void updateWaterContract(Watercontract w) {
        tx.begin();
        em.createNativeQuery("update watercontract set company='"+w.getCompany()+"' where contractFK='"+w.getContract().getIdContract()+"' and idWaterContract='"+w.getIdWaterContract()+"'").executeUpdate();
        tx.commit();
    }
}
